/**
 * Created by yqj on 2017/2/4.
 */
import { contentManagementResource } from '../../api'

export const contentManagementStore = {
  state: {
    bookList: [],
    textBookList: [],
    pageSize: 10,
    currentPage: 1,
    totalSize: 0
  },
  actions: {
    FETCH_BOOK_LIST_DATA_OF_CONTENT_MANAGEMENT ({ commit, dispatch, state }, { status, nameOrNumberOrISBN }) {
      return contentManagementResource.getBookList({pageNumber: state.currentPage, pageSize: state.pageSize, nameOrNumberOrISBN: nameOrNumberOrISBN || null, bookStatus: status || null})
        .then(response => {
          let list = response.body.result.content
          let totalSize = response.body.result.totalSize
          commit('SET_BOOK_LIST', { list })
          commit('SET_BOOK_LIST', { list })
          commit('SET_TOTAL_SIZE', { totalSize })
        })
    },
    FETCH_TEXT_BOOK_LIST_DATA_OF_CONTENT_MANAGEMENT ({ commit, dispatch, state }, payload) {
      return contentManagementResource.getTextBookList({pageNumber: state.currentPage, pageSize: state.pageSize, ...payload})
        .then(response => {
          let list = response.body.result.content
          let totalSize = response.body.result.totalSize
          commit('SET_TEXT_BOOK_LIST', { list })
          commit('SET_TOTAL_SIZE', { totalSize })
        })
    },
    INIT_PAGINATION_STATUS_CONTENT_MANAGEMENT ({ commit, dispatch, state }) {  // 初始化分页状态
      commit('INIT_PAGINATION_STATUS_CONTENT_MANAGEMENT')
    },
    CHANGE_CURRENT_PAGE_CONTENT_MANAGEMENT ({ commit, dispatch, state }, { currentPage }) {  // 改变当前页
      commit('SET_CURRENT_PAGE_CONTENT_MANAGEMENT', { currentPage })
    },
    CHANGE_PAGE_SIZE_CONTENT_MANAGEMENT ({commit, dispatch, state}, { pageSize }) {  // 改变每页展示数量
      commit('SET_PAGE_SIZE_CONTENT_MANAGEMENT', { pageSize })
    }
  },
  mutations: {
    SET_BOOK_LIST (state, { list }) {
      state.bookList = list
    },
    SET_TEXT_BOOK_LIST (state, { list }) {
      state.textBookList = list
    },
    INIT_PAGINATION_STATUS_CONTENT_MANAGEMENT (state) {   // 初始化分页状态
      state.currentPage = 1
      state.pageSize = 10
      state.totalSize = 0
    },
    SET_CURRENT_PAGE_CONTENT_MANAGEMENT (state, { currentPage }) {  // 设置当前页
      state.currentPage = currentPage
    },
    SET_PAGE_SIZE_CONTENT_MANAGEMENT (state, { pageSize }) {   // 设置每页展示数量
      state.pageSize = pageSize
    },
    SET_TOTAL_SIZE (state, { totalSize }) {    // 设置数据总条数
      state.totalSize = totalSize
    }
  },
  getters: {

  }
}
